package com.coding.controller;



import com.alibaba.fastjson2.JSON;
import com.coding.dto.RestBean;
import com.coding.pojo.UserIdInfo;
import com.coding.service.UserService;
import com.coding.util.ImageCodeUtils;
import com.coding.util.RandomUtil;
import com.coding.util.ResponseDTO;
import com.coding.util.SendSms;
import okhttp3.*;
import okhttp3.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

//@CrossOrigin
@RestController
public class id2demoController {

    @Autowired
    UserService userService;

    String url = "https://idenauthen.market.alicloudapi.com/idenAuthentication";
    // 获取appCode链接：https://market.console.aliyun.com/
    String appCode = "0605edc5eebd4208b2f842685a4b1d86";

    // 实名验证接口
    @RequestMapping("/id2demo")
    public RestBean<String> captcha(String name, String idNo,long userId) throws IOException, ExecutionException, InterruptedException {
        System.out.println("-----------------"+userId);
        if (name == null || idNo == null || name=="" || idNo==""){
            System.out.println("输入错误，数值为空");
            return RestBean.failure(400,"身份认证失败");
        }
        System.out.println(postData(appCode, url, name, idNo));
        String code = postData(appCode, url, name, idNo);
        UserIdInfo parse =JSON.parseObject(code,UserIdInfo.class);
        System.out.println(parse);
        if (parse.getRespCode().equals("0000")){
            RestBean authentication = userService.authentication(parse.getIdNo(), userId);
            if (authentication.getCode()==200){
                return RestBean.success("身份认证成功");
            }
        }
        return RestBean.failure(400,"身份认证失败");
    }
    public static String postData(String appCode, String url, String name, String idNo) throws IOException {
        String result = "";
        RequestBody formBody = new FormBody.Builder().add("name", name).add("idNo", idNo).build();
        Request request = new Request.Builder().url(url).addHeader("Authorization", "APPCODE " + appCode).post(formBody).build();

        Call call = new OkHttpClient().newCall(request);
        Response response = null;
        try {
            response = call.execute();
        } catch (IOException e) {
            System.out.println("execute failed, message:" + e.getMessage());
        }

        assert response != null;
        if (!response.isSuccessful()) {      // 当返回结果发生错误时
            // 状态码为403时一般是套餐包用尽，需续购；注意：续购不会改变秘钥（appCode），仅增加次数
            // 续购链接：https://market.aliyun.com/products/57000002/cmapi025518.html
            System.out.println("request failed----" + "返回状态码" + response.code() + ",message:" + response.message());
        }
        result = response.body().string();    //此处不可以使用toString()方法，该方法已过期

        return result;
    }
}
